import java.util.HashMap;

//存在重复元素II
//https://leetcode.cn/problems/contains-duplicate-ii/description/
public class Test {
    public static void main(String[] args) {
        //
    }
}


class Solution {
    public boolean containsNearbyDuplicate(int[] nums, int k) {
        //
        HashMap<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            if (map.containsKey(nums[i])) {
                int m = map.get(nums[i]);
                int abs = Math.abs(i - m);
                if(abs<=k){
                    return true;
                }
                map.put(nums[i], i);
            }else{
                map.put(nums[i], i);
            }
        }
        return false;
    }
}
